﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using log4net;
using CashFlow.Db.Entity;
using SysAdmin._00_Def;
using SysAdmin._03_Service._00_Db;
using SysAdmin._04_Form._00_Common;

namespace SysAdmin._04_Form._01_Shop
{
    public partial class ShopUserForm : Form
    {
        private static ILog _logger = LogManager.GetLogger(typeof(ShopUserForm));
        private IList<MyShopUserInfo> ShopMemberUserList { get; set; }
        private Timer _timer = new Timer();
        public int ShopManagerUserId { get; set; }
        public string ShopName { get; set; }

        public ShopUserForm()
        {
            InitializeComponent();
        }

        private void ShopUserForm_Load(object sender, EventArgs e)
        {
            label6.Text = string.Format("{0}成员列表", ShopName);

            dataGridView1.AutoGenerateColumns = false;
            label1.Text = "";

            _timer.Interval = 100;
            _timer.Tick += _timer_Tick;
        }

        private void ShopUserForm_Shown(object sender, EventArgs e)
        {
            _timer.Start();
        }

        private void _timer_Tick(object sender, EventArgs e)
        {
            try
            {
                _timer.Stop();
                ShopMemberUserList = ShopUserService.QueryAllShopUsers(ShopManagerUserId);
                LoadShopUsers(false);
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                UICommonTool.ShowAlertDialog(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LoadShopUsers(false);
        }

        private void LoadShopUsers(bool reload)
        {
            if (reload)
            {
                try
                {
                    ShopMemberUserList = ShopUserService.QueryAllShopUsers(ShopManagerUserId);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                    UICommonTool.ShowAlertDialog(ex.Message);
                    return;
                }
            }

            var ls = ShopMemberUserList.Where(x => x.UserInfo.Name.Contains(textBox1.Text.Trim()) &&
                        x.UserInfo.InvitationCode.Contains(textBox2.Text.Trim()) &&
                        x.ReferenceUserInfo.Name.Contains(textBox3.Text.Trim()) &&
                        x.UserInfo.ReferenceInvitationCode.Contains(textBox4.Text.Trim()))
                    .ToList();

            int index = 0;
            var ls2 = ls.Select(x => new GridDataItemData
            {
                No = (++index).ToString().PadLeft(2, '0'),
                Id = x.ShopUserInfo.Id,
                CreateTime = x.UserInfo.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                InvitationCode = x.UserInfo.InvitationCode,
                Name = x.UserInfo.Name,
                Reference = x.ReferenceUserInfo.Name,
                ReferenceInvitationCode = x.UserInfo.ReferenceInvitationCode
            }).ToList();

            dataGridView1.DataSource = ls2;
            label1.Text = string.Format("成员人数: {0}", ls.Count());
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex == 7)
                {
                    if (UICommonTool.ShowConfirmDialog("确定要删除吗？"))
                    {
                        string id = (string)dataGridView1.Rows[e.RowIndex].Cells[0].FormattedValue;
                        int shopUserId = Convert.ToInt32(id);
                        bool b = ShopUserService.DeleteShopUserById(shopUserId);
                        if (b)
                        {
                            UICommonTool.ShowInfoDialog("删除成功！");
                            LoadShopUsers(true);
                        }
                        else
                        {
                            UICommonTool.ShowAlertDialog("删除失败！");
                        }
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var form = new AddShopUserForm();
            form.ShopManagerUserId = ShopManagerUserId;
            DialogResult dr = form.ShowDialog();
            if (dr == DialogResult.OK)
            {
                LoadShopUsers(true);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                var form = new UpdateShopUserForm();
                form.ShopManagerUserId = ShopManagerUserId;
                var dr = form.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    LoadShopUsers(true);
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                UICommonTool.ShowAlertDialog(ex.Message);
            }
        }

        private class GridDataItemData
        {
            public string No { get; set; }
            public int Id { get; set; }
            public string Name { get; set; }
            public string InvitationCode { get; set; }
            public string Reference { get; set; }
            public string ReferenceInvitationCode { get; set; }
            public string CreateTime { get; set; }
        }
    }
}
